package cn.tedu.v1;

import java.io.*;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    private ServerSocket serverSocket;
    public Server() {
        try {
            serverSocket=new ServerSocket(1234);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void start() throws IOException {
        System.out.println("----等待客户端链接----");
        Socket socket=serverSocket.accept();
        System.out.println("一个客户端链接成功了~~");
        InputStream is=socket.getInputStream();
        InputStreamReader isr=new InputStreamReader(is,"UTF8");
        BufferedReader br=new BufferedReader(isr);
        PrintWriter out=new PrintWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF8"),true);
        String str=null;
        while(true){
            str=br.readLine();
            if(str==null){
                break;
            }
            System.out.println("客户端说的话："+str);

            out.println("服务器收到："+str);
        }
        //关闭资源
        br.close();
        out.close();
        socket.close();
        serverSocket.close();
    }

    public static void main(String[] args) throws IOException {

        Server server=new Server();
        server.start();
    }
}